//
//  LoginViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/8.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPwdController.h"
#import "MyViewController.h"
#import "AFNetworkHandler.h"
#import "WholesaleViewController.h"
#import "MyCommunity.h"
@interface LoginViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIButton *btn1;//登录
@property (nonatomic,strong) UIButton *btn2;//忘记密码
@property (nonatomic,strong) UIButton *btn3;//商家版是什么
@property (nonatomic,strong) UITextField *phoneNumTextField;//账号框
@property (nonatomic,strong) UITextField *pswTextField;//密码框

@end

@implementation LoginViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [AppData share].doFirst = NO;
    [NSUserDefualManager storageValue:[NSNumber numberWithBool:YES] forKey:@"DoFirstLogin"];
    if (![AppData share].doFirst) {
        NSString *name = [NSUserDefualManager getValueByKey:UserName];
        _phoneNumTextField.text = name;
        NSLog(@"11111");
    }else {
        NSLog(@"22222");
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"" doBack:NO];
    //添加scrollView
//    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
//    self.scrollView.delegate = self;
//    [self.view addSubview:self.scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    titleLabel.text = @"懒猫社长商家版";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    [self.navBar addSubview:titleLabel];
    self.navBar.backgroundColor = MainColor;
    //键盘控制弹起与收回
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHident:) name:UIKeyboardWillHideNotification object:nil];
    //主界面的搭建
    //上面图片
    UIImageView *adImageV = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*0.3, 94, ScreenWidth*0.4, ScreenWidth*0.625*0.4)];
    adImageV.image = [UIImage imageNamed:@"denglulogo.png"];
    [self.view addSubview:adImageV];
    //第一块
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenWidth*0.625*0.4+124, ScreenWidth, 50)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    UIView *lineView0 = [[UIView alloc]init];
    lineView0.frame = CGRectMake(0, 0, ScreenWidth, 1);
    lineView0.backgroundColor = [UIColor lightGrayColor];
    lineView0.alpha = 0.2;
    [view1 addSubview:lineView0];
    UIImageView *dlImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 18, 18)];
    dlImageView1.image = [UIImage imageNamed:@"denglu42_42"];
    [view1 addSubview:dlImageView1];
    self.phoneNumTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, 8, view1.frame.size.width-35, 34)];
    self.phoneNumTextField.placeholder = @"请输入账号";
    self.phoneNumTextField.delegate = self;
    self.phoneNumTextField.tag = 100;
    _phoneNumTextField.textAlignment = NSTextAlignmentLeft;
    _phoneNumTextField.font = [UIFont systemFontOfSize:16];
    _phoneNumTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view1 addSubview:_phoneNumTextField];
    UIView *lineView = [[UIView alloc]init];
    lineView.frame = CGRectMake(10, 49, ScreenWidth-20, 1);
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [view1 addSubview:lineView];
    //第二块
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenWidth*0.625*0.4+174, ScreenWidth, 50)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    UIImageView *dlImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 18, 20)];
    dlImageView2.image = [UIImage imageNamed:@"mima.png"];
    [view2 addSubview:dlImageView2];
    self.pswTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, 8, view2.frame.size.width-35, 34)];
    _pswTextField.placeholder = @"请输入密码";
    self.pswTextField.delegate = self;
    self.pswTextField.tag = 200;
    _pswTextField.textAlignment = NSTextAlignmentLeft;
    _pswTextField.font = [UIFont systemFontOfSize:16];
    _pswTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _pswTextField.secureTextEntry = YES;
    [view2 addSubview:_pswTextField];
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.frame = CGRectMake(0, 49, ScreenWidth, 1);
    lineView2.backgroundColor = [UIColor lightGrayColor];
    lineView2.alpha = 0.2;
    [view2 addSubview:lineView2];
    //第三块
    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn1.tag = 100;
    self.btn1.layer.cornerRadius = 5.0;
    self.btn1.layer.masksToBounds = YES;
    self.btn1.backgroundColor = MainColor;
    self.btn1.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.btn1.frame = CGRectMake(15, ScreenWidth*0.625*0.4+249, ScreenWidth-30, 50);
    [self.btn1 setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:self.btn1];
    [self.btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.btn2.tag = 101;
//    self.btn2.titleLabel.font = [UIFont systemFontOfSize:16];
//    [self.btn2 setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:UIControlStateNormal];
//    self.btn2.frame = CGRectMake(10, ScreenWidth*0.625*0.4+310, 90, 21);
//    [self.btn2 setTitle:@"忘记密码？" forState:UIControlStateNormal];
//    [self.view addSubview:self.btn2];
//    [self.btn2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.btn3.tag = 102;
//    self.btn3.titleLabel.font = [UIFont systemFontOfSize:16];
//    [self.btn3 setTitleColor:[UIColor colorWithRed:0 green:170/255.0 blue:237/255.0 alpha:1.0] forState:UIControlStateNormal];
//    self.btn3.frame = CGRectMake(ScreenWidth-120, ScreenWidth*0.625*0.4+310, 120, 21);
//    [self.btn3 setTitle:@"商家版是什么？" forState:UIControlStateNormal];
//    [self.view addSubview:self.btn3];
//    [self.btn3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (ScreenHeight == 480) {
        CGPoint center = self.view.center;
        center.y = ScreenHeight/2-80;
        [UIView animateWithDuration:0.2 animations:^{
            //设置要被动画的那个视图
            //在动画结束时，它的属性处于什么状态
            self.view.center = center;
        }];
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (ScreenHeight == 480) {
        CGPoint center = self.view.center;
        center.y = ScreenHeight/2;
        [UIView animateWithDuration:0.2 animations:^{
            //设置要被动画的那个视图
            //在动画结束时，它的属性处于什么状态
            self.view.center = center;
        }];
    }
}
#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
#pragma mark - 通知事件
- (void)keyboardWillShow:(NSNotification *)notifi {
    NSDictionary* info = [notifi userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-kbSize.height);
}
- (void)keyboardWillHident:(NSNotification *)notifi {
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64);
}
- (IBAction)tapHidentKB:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}
//按钮的点击事件
-(void)click:(UIButton *)sender {
//    NSInteger tag = sender.tag;
//    if (tag == 100) {//登录
        [self.view endEditing:YES];
        [self login];
//    }else if (tag == 101) {//忘记密码
//        ForgetPwdController *forgetVC1 = [[ForgetPwdController alloc] init];
//        forgetVC1.strTitle = @"忘记密码";
//        forgetVC1.forgetPwdUrl = FinePsw;
//        [self presentViewController:forgetVC1 animated:YES completion:^{
//        }];
//    }else {//点击商家版是什么
//        ForgetPwdController *forgetVC2 = [[ForgetPwdController alloc] init];
//        forgetVC2.strTitle = @"商家版是什么";
//        forgetVC2.forgetPwdUrl = businessDetail;
//        
//        [self presentViewController:forgetVC2 animated:YES completion:^{
//        }];
//    }
}
#pragma mark -login
- (void)login {
    if (_phoneNumTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"账号不能为空" login:NO];
        return;
    }
    if (_pswTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"密码不能为空" login:NO];
        return;
    }
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *cid = [userDefaul objectForKey:@"cid"];
    
    //    NSString *cid = [NSUserDefualManager getValueByKey:@"cid"];
    NSLog(@"=-----%@",cid);
    NSString *url = [NSString stringWithFormat:businessLoginUrl,_phoneNumTextField.text,_pswTextField.text,cid];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        LCLog(@"登录成功后----%@",dic);
        
        /**
         *  saleOrLanmao..
         */
        NSString *isLanmao = dic[@"islanmao"];
        NSString *r = dic[@"r"];
        
        if ([r isEqualToString:@"T"])
        {
            NSDictionary *userMess = dic[@"shopinfo"];
            NSDictionary *userMess111 = dic[@"scoreinfo"];
            NSArray *communityArrs = userMess[@"communitys"];
            NSMutableArray *communityNameArs = [NSMutableArray array];
            communityNameArs = [MyCommunity mj_objectArrayWithKeyValuesArray:communityArrs];
            [AppData share].communityArrs = [communityNameArs mutableCopy];
            
            if ([isLanmao isEqualToString:@"0"]) {//批发平台.
                NSDictionary *infoDic = [dic objectForKey:@"scoreinfo"];
                [AppData share].MonthBuyCount = [infoDic objectForKey:@"totalmonamt"];
                [AppData share].TodayBuyCount = [infoDic objectForKey:@"totaldayamt"];
                
                
                [AppData share].doLogin = YES;
                [NSUserDefualManager storageValue:_phoneNumTextField.text forKey:UserName];
                [AppData share].username = _phoneNumTextField.text;
                [NSUserDefualManager storageValue:_pswTextField.text forKey:Psw];
                [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
                
                [NSUserDefualManager storageValue:isLanmao forKey:@"isLanmao"];
                WholesaleViewController *wholeSaleVC = [[WholesaleViewController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:wholeSaleVC];
                nav.navigationBarHidden = YES;
                [self presentViewController:nav animated:YES completion:nil];
            }else {//商家版
                NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults]; // 蓝牙开关
                [userDefaul setObject:@"off" forKey:@"switch"];
                /***********懒猫掌柜****************/
                [AppData share].doLogin = YES;
                [AppData share].loginPhone = _phoneNumTextField.text;
                [AppData share].loginPsw = _pswTextField.text;
                [AppData share].mobile = userMess[@"mobile"];
                [AppData share].username = userMess[@"username"];
                [AppData share].shoplogo = userMess[@"shoplogo"];
                [AppData share].shopname = userMess[@"shopname"];
                [AppData share].contacter = userMess[@"contacter"];
                [AppData share].shortintroduce = userMess[@"shortintroduce"];
                [AppData share].shopnote = userMess[@"shopnote"];
                [AppData share].openingend = userMess[@"openingend"];
                [AppData share].openingstart = userMess[@"openingstart"];
                [AppData share].serveramount = userMess[@"serveramount"];
                [AppData share].minSentamount = userMess[@"minSentamount"];
                [AppData share].privatekey = userMess[@"privatekey"];
                [AppData share].totalincome = userMess111[@"totalincome"];
                [AppData share].cashbalance = userMess111[@"cashbalance"];
                [NSUserDefualManager storageValue:_phoneNumTextField.text forKey:UserName];
                [NSUserDefualManager storageValue:_pswTextField.text forKey:Psw];
                [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
                [NSUserDefualManager storageValue:@"1" forKey:@"lingsheng"];
                [NSUserDefualManager storageValue:@"1" forKey:@"zhendong"];
                [NSUserDefualManager storageValue:isLanmao forKey:@"isLanmao"];
                
                //跳转
                [[AppData share].baseTabVC theShoppingCartCount:userMess[@"shopcart"]];
                //获取个人信息
                [NTNetUtil getUserMess];
                BaseTabBarViewController *baseTab = [[BaseTabBarViewController alloc] init];
                [baseTab changeBottomBtnWithIndex:0];
                [self presentViewController:baseTab animated:YES completion:nil];
            }
        }else
        {
            [[MessShowView share] showWithMess:@"账户名或密码错误" login:NO];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
